-- d is whether to draw the button or not
function gui.ImageButton(t, s, p, image, d, f)

  --local i = love.graphics.newImage("resources/images/intro/OilDrum.png")
  gui.EasyImage(image, p[1], p[2], s[1], s[2])



  --print(d)

  if gui.Button{text = t, size = s, pos = p, drw = d} then
    f()
  end
  
  

  local w = image:getWidth()
  local h = image:getHeight()
  if mouseIsTouching({p[1], p[2], s[1], s[2]}) then

    if love.mouse.isDown( "l" ) then
      love.graphics.setColor(0,0,0,4)
    else
       love.graphics.setColor(255,255,255,4)
    end

    love.graphics.rectangle("fill", p[1], p[2], s[1], s[2] )
  end

end

function gui.EasyImage(image, x,y,w,h,c, d)
  -- -1 for auto
  if w and w==-1 then w = image:getWidth() end
  if h and h==-1 then h = image:getHeight() end

  local scale = scaleToSize({w,h}, image)
  local s1 = scale[1]
  local s2 = scale[2]

  if c then 
    x = x - w/2
    y = y - h/2
  end

  if not table.HasValue(gui.layer, {x,y,w,h}) then
    table.insert(gui.layer, {x,y,w,h})
  end

  if d then 
    love.graphics.draw(image, x, y, math.rad(d), s1, s2)
  else
    love.graphics.draw(image, x, y, 0, s1, s2)
  end
end

function scaleToSize(pos, image)
  local width = pos[1]
  local height = pos[2]
  return {(width/image:getWidth()),(height/image:getHeight())}
end

function mouseIsTouching(xywh)
  local mx = love.mouse.getX()
  local my = love.mouse.getY()
  local x = xywh[1]
  local y = xywh[2]
  local w = xywh[3]
  local h = xywh[4]

  if (mx>x and mx<x+w) and (my>y and my<y+h) then
    return true
  else 
    return false
  end
end



function mouseIsTouchingLayer(xywhtable)
  local hit = false
  for i, v in pairs(xywhtable) do
    local mx = love.mouse.getX()
    local my = love.mouse.getY()
    local x = v[1]
    local y = v[2]
    local w = v[3]
    local h = v[4]

    if (mx>x and mx<x+w) and (my>y and my<y+h) then
      hit = true
    end
  end
  return hit
end